using System;
using System.Collections;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Windows.Forms;
using OpenComposite.EII.Designer;

namespace OpenComposite.EII
{
	public class RepositorySurface : DesignSurface
	{
		#region Initialization
		public RepositorySurface() : base()
		{
            this.AddService(typeof(IMenuCommandService), new MenuCommandService(this));
		}
		public RepositorySurface(IServiceProvider parentProvider)
			: base(parentProvider)
		{
            this.AddService(typeof(IMenuCommandService), new MenuCommandService(this));
        }

		internal void Initialize(IController controller)
		{
			//Control control = null;
			_controller = controller;
			IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));

			if (host == null)
				return;

			try
			{
				// Set SelectionService - SelectionChanged event handler
				//_selectionService = (ISelectionService)(this.ServiceContainer.GetService(typeof(ISelectionService)));
				//_selectionService.SelectionChanged += new EventHandler(selectionService_SelectionChanged);
			}
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }
		#endregion

		#region Public Members

		#region Properties
		public IController Controller
		{
			get { return _controller; }
		}
		#endregion

		#region Methods
		public void AddService(Type type, object serviceInstance)
		{
			this.ServiceContainer.AddService(type, serviceInstance);
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		//private ISelectionService _selectionService;
		private IController _controller;
		#endregion

		#region Event Handlers
		/// <summary>
        /// When the selection changes this sets the PropertyGrid's selected component 
		/// </summary>
        private void selectionService_SelectionChanged(object sender, EventArgs e)
		{
			//if (_selectionService != null)
			//{
			//    ICollection selectedComponents = _selectionService.GetSelectedComponents();
			//    PropertyGrid propertyGrid = (PropertyGrid)this.GetService(typeof(PropertyGrid));

			//    object[] comps = new object[selectedComponents.Count];
			//    int i = 0;

			//    foreach (Object o in selectedComponents)
			//    {
			//        comps[i] = o;
			//        i++;
			//    }

			//    propertyGrid.SelectedObjects = comps;
			//}
		}
		#endregion

		#endregion
	}
}
